package com.cosina.game.robodefence.core;

import java.util.Iterator;
import java.util.LinkedList;

import com.cosina.game.robodefence.model.Enemy;
import com.cosina.game.robodefence.util.Time;
import com.cosina.game.robodefence.util.UpCount;

public class EnemyManager implements Time{
	public LinkedList<Enemy> enemies = new LinkedList<Enemy>();
	private Wave wave;
	UpCount waveInternal = new UpCount(60);
	UpCount outInternal = new UpCount(10);
	@Override
	public void withTimeGo() {
		Iterator<Enemy> enemiesIter = enemies.iterator();
		while(enemiesIter.hasNext()){
			Enemy enemy = enemiesIter.next();
			enemy.move();
			if(enemy.isArrive()){
				enemiesIter.remove();
				BattleEngine.commander.hit();
			}
		}
		
		if(wave == null){
			if(enemies.size() == 0){
				if(waveInternal.upToAlram() ){
					wave = BattleEngine.waveManager.getNextWave();
					if(null == wave){
						BattleEngine.status = BattleEngine.Status.over;
					}
					BattleEngine.commander.newWave();
				}
			}
		}else{
			if(outInternal.upToAlram()){
				Enemy enemy = wave.getNext();
				enemies.add(enemy);
				if(wave.isEmpty()){
					wave = null;
				}
			}
		}
	}
	
	public void updateEnemyWayWhenBuild(int x, int y){
		Iterator<Enemy> enemiesIter = enemies.iterator();
		while(enemiesIter.hasNext()){
			enemiesIter.next().way.updateIfCross(x, y);
		}
	}
	
	public void updateEnemyWayWhenDemolition(){
		Iterator<Enemy> enemiesIter = enemies.iterator();
		while(enemiesIter.hasNext()){
			enemiesIter.next().way.update();
		}
	}
}
